<?php

namespace App\Admin\Controllers;

use App\model\customer\CustomerBigDepart;
use App\model\customer\CustomerSecondDepart;
use App\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class CustomerSecondDepartController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '销售小组';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new CustomerSecondDepart);

        $grid->column('id', __('Id'));
        $grid->column('name', __('小组名称'));
        $grid->column('sort', __('排序'));
        $grid->column('parent_id', __('所属大组'))->display(function ($val){
           return CustomerBigDepart::find($val)->name??"";
        });
        $grid->column('user_ids', __('小组成员'))->display(function ($value) {
            return User::whereIn('id',$value)->select('id','name')->get();
        })->pluck('name')->map('ucwords')->label('success');
        //$grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('更新时间'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(CustomerSecondDepart::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('sort', __('Sort'));
        $show->field('parent_id', __('Parent id'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new CustomerSecondDepart);

        $form->text('name', __('小组名称'));
        $form->number('sort', __('排序'));
        $form->select('parent_id', __('所属大组'))->options(CustomerBigDepart::all()->pluck('name', 'id'));
        $form->multipleSelect('user_ids', __('小组成员'))->options(User::all()->pluck('name', 'id'));
        $form->saving(function (Form $form){
            $model_id = $form->model()->id;
            if($model_id!=null){
                $user_ids = CustomerSecondDepart::find($model_id)->user_ids;
                User::whereIn('id',$user_ids)->update(['department_id'=>null]);
            }
        });
        $form->saved(function (Form $form) {
            $users = CustomerSecondDepart::where('user_ids',$form->user_ids);
            foreach ($form->user_ids as $ids){
                $datas = \DB::select('select * from customer_second_depart where FIND_IN_SET(?,user_ids)',[$ids]);
                foreach ($datas as $data){
                    $par_id = $data->id;
                    if($par_id != $form->model()->id){
                        $user_ids = explode(',',$datas[0]->user_ids);
                        $array = array_diff($user_ids, [$ids]);
                        CustomerSecondDepart::where('id',$par_id)->update(['user_ids'=>implode(',',$array)]);
                    }
                }
            }
            User::whereIn('id',$form->user_ids)->update(['department_id'=>$form->model()->id]);
        });
        return $form;
    }
}
